<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>构造函数参数验证</title>
</head>
<body>
  <script>
    class User {
      constructor(id) {
        this.id_ = id;
      }
    }

    const proxy = new Proxy(User, {
      construct(target, argumentsList, newTarget) {
        if (argumentsList[0] === undefined) {
          throw 'User cannot be instantiated without id';
        } else {
          return Reflect.construct(...arguments);
        }
      }
    });

    new proxy(1);

    new proxy();
    // Error: User cannot be instantiated without id
  </script>
</body>
</html>